﻿/**********************************天使物流*******************************************
* 功能描述:结算管理-销进项票管理
* 作者:石锦涛
* 时间:2022-01-11
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.SettleAccounts.NewSalesInvoices;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.SettleAccounts.NewSalesInvoices
{
    public class NewSalesAppRepository : DapperDbContextRepository, INewSalesAppRepository
    {
        public NewSalesAppRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<NewSalesInvoiceDto>> GetNewSales(string name, int? clas, string times, int pageindex = 1, int pagesize = 5)
        {
            try
            {
                var sql = "select Id as GId,PinBanks,InvoiceUnitx,InvoiceType,InvoiceMoneyx,TaxRatex,TaxQuantityx,SUBSTRING(CONVERT(Nvarchar(50),MakeInvoiceTimex),1,10) as MakeInvoiceTimex,Remarkzx,PreparedDyrx,SUBSTRING(CONVERT(Nvarchar(50), EstablishTimes), 1, 10) as EstablishTimes,IsDelete from NewSalesInvoice where IsDelete = 0    ";
                if (!string.IsNullOrEmpty(name))
                {
                    sql += $" and InvoiceUnitx like '%{name}%'";
                }
                if (clas > 0)
                {
                    sql += $" and InvoiceTypex = '{clas}'";
                }
                if (!string.IsNullOrEmpty(times))
                {
                    sql += $" and MakeInvoiceTimex = '{times}'";
                }

                PageResponse<NewSalesInvoiceDto> doc = new PageResponse<NewSalesInvoiceDto>();
                var list = await this.QueryAsync<NewSalesInvoiceDto>(sql, null);
                doc.PageTotal = list.Count();
                sql += $" order by Id offset ({pageindex}-1)*{pagesize} rows fetch next {pagesize} rows only";
                var query = await this.QueryAsync<NewSalesInvoiceDto>(sql, null);
                doc.PageData = query;
                return doc;

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        ///  销项发票逻辑删除
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<NewSalesInvoiceDto>> DeleteNewSales(int Id)
        {
            try
            {
                PageResponse<NewSalesInvoiceDto> doc = new PageResponse<NewSalesInvoiceDto>();
                var sql = $"update  NewSalesInvoice set IsDelete=1 where Id={Id}";
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改销项发票
        /// </summary>
        /// <param name="newInput"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> UpdateNewSales(NewSalesInvoiceDto newSales)
        {
            try
            {
                var sql = $"update  NewSalesInvoice set PinBanks='{newSales.PinBanks}',InvoiceUnitx='{newSales.InvoiceUnitx}',InvoiceType='{newSales.InvoiceType}',InvoiceMoneyx='{newSales.InvoiceMoneyx}',TaxRatex='{newSales.TaxRatex}',TaxQuantityx = '{newSales.TaxQuantityx}',MakeInvoiceTimex = '{newSales.MakeInvoiceTimex.ToString("yyyy/MM/dd")}',Remarkzx = '{newSales.Remarkzx}',PreparedDyrx = '{newSales.PreparedDyrx}',EstablishTimes = '{newSales.EstablishTimes.ToString("yyyy/MM/dd")}',IsDelete = '{newSales.IsDelete}' where Id = '{newSales.GId}'";
                PageResponse<int> doc = new PageResponse<int>();

                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增销项发票
        /// </summary>
        /// <param name="newInput"></param>
        /// <returns></returns>
        public async Task<PageResponse<int>> PostNewSales(NewSalesInvoiceDto newSales)
        {
            try
            {
                //雪花算法
                var worker = new IdWorker(1, 1);
                var id = worker.NextId().ToString();

                var sql = $"insert NewSalesInvoice values('{id}','{newSales.InvoiceUnitx}','{newSales.InvoiceType}','{newSales.InvoiceMoneyx}','{newSales.TaxRatex}','{newSales.TaxQuantityx}','{newSales.MakeInvoiceTimex.ToString("yyyy/MM/dd")}','{newSales.Remarkzx}','{newSales.PreparedDyrx}',GETDATE(),'{newSales.IsDelete}')";
                PageResponse<int> doc = new PageResponse<int>();
                doc.Code = await this.ExecuteAsync(sql, null);
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除销项发票
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<PageResponse<NewSalesInvoiceDto>> DeleteNewSalesMent(string Id)
        {
            try
            {
                PageResponse<NewSalesInvoiceDto> doc = new PageResponse<NewSalesInvoiceDto>();
                var ids = Id.Split(',');
                foreach (var item in ids)
                {
                    var shu = Convert.ToInt32(item);
                    var list = await this.QueryAsync<NewSalesInvoiceDto>($"select * from NewSalesInvoice where Id={shu}");
                    if (list != null)
                    {
                        var sql = $"update NewSalesInvoice set IsDelete=1 where Id={shu} ";
                        doc.Code = await this.ExecuteAsync(sql, null);
                    }
                }
                return doc;
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}
